package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.epg.EpgAggregationCategoryMapper;
import com.ztgf.shopping.models.bo.epg.EpgAggregationCategoryBO;
import com.ztgf.shopping.models.bo.epg.EpgAggregationCategorySaveBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.dto.epg.EpgAggregationCategoryDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.epg.IEpgAggregationCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聚合页分类控制器
 *
 * @author YUE
 * @Date 2020-06-04 15:38:08
 */
@DubboService
public class EpgAggregationCategoryServiceImpl extends ServiceImpl<EpgAggregationCategoryMapper, EpgAggregationCategoryDTO> implements IEpgAggregationCategoryService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgAggregationCategoryMapper epgAggregationCategoryMapper;

    @Override
    public boolean add(EpgAggregationCategorySaveBO saveBO) {
        EpgAggregationCategoryDTO dto = new EpgAggregationCategoryDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 创建默认的店铺活动页分类
     * @param templateId
     * @param moduleId
     */
    public void createDefaultShopActiveSection(Long templateId,Long moduleId){
        EpgAggregationCategorySaveBO epgAggregationCategorySaveBO = new EpgAggregationCategorySaveBO();
        epgAggregationCategorySaveBO.setTempletId(templateId);
        epgAggregationCategorySaveBO.setModuleId(moduleId);
        epgAggregationCategorySaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationCategorySaveBO.setName("");
        //分类1
        epgAggregationCategorySaveBO.setSort(0);
        epgAggregationCategorySaveBO.setName("热卖爆品");
        add(epgAggregationCategorySaveBO);
        //分类2
        epgAggregationCategorySaveBO.setSort(1);
        epgAggregationCategorySaveBO.setName("热卖盾构");
        add(epgAggregationCategorySaveBO);
        //分类3
        epgAggregationCategorySaveBO.setSort(2);
        epgAggregationCategorySaveBO.setName("旗舰配件");
        add(epgAggregationCategorySaveBO);
    }

    public Long addAndReturnId(EpgAggregationCategorySaveBO saveBO) {
        EpgAggregationCategoryDTO dto = new EpgAggregationCategoryDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        this.insert(dto);
        return id;
    }

    /**
     * 根据模板id获取所有分类信息
     * @param templateId
     * @return
     */
    public List<EpgAggregationCategoryDTO> getByTemplateId(Long templateId){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("templet_id", templateId);
        return selectList(wrapper);
    }


    /**
     * 删除功能功能相关的活动banner
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgAggregationCategoryMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 删除功能功能相关的活动banner
     * @return
     */
    public boolean deleteByTemplate(Long templateId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("templet_id",templateId);
        return epgAggregationCategoryMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据相关信息查找 banner 有页码
     * @param epgAggregationCategoryBO
     * @return
     */
    @Override
    public Page<EpgAggregationCategoryDTO> getBySelectCategoryInfoPage(EpgAggregationCategoryBO epgAggregationCategoryBO) {
        epgAggregationCategoryBO = addMustData(epgAggregationCategoryBO);
        Wrapper<EpgAggregationCategoryDTO> wrapper = getBySelectCategoryInfo(epgAggregationCategoryBO);
        return selectPage(new Page<>(epgAggregationCategoryBO.getPageNo(),epgAggregationCategoryBO.getPageSize()), wrapper);
    }

    /**
     * 添加必要数据
     * @param epgAggregationCategoryBO
     * @return
     */
    private EpgAggregationCategoryBO addMustData(EpgAggregationCategoryBO epgAggregationCategoryBO){
        epgAggregationCategoryBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationCategoryBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgAggregationCategoryBO.setSort(ShoppingCodeEnum.SORT_ASC.getCode());
        return epgAggregationCategoryBO;
    }

    /**
     * 根据相关信息查找 banner 无页码
     * @param epgAggregationCategoryBO
     * @return
     */
    @Override
    public List<EpgAggregationCategoryDTO> getBySelectCategoryInfoNoPage(EpgAggregationCategoryBO epgAggregationCategoryBO) {
        epgAggregationCategoryBO = addMustData(epgAggregationCategoryBO);
        Wrapper<EpgAggregationCategoryDTO> wrapper = getBySelectCategoryInfo(epgAggregationCategoryBO);
        if(epgAggregationCategoryBO.getPageNo() == 0 && epgAggregationCategoryBO.getPageSize() == 0)
            return epgAggregationCategoryMapper.selectPage( new Page<>(0, ShoppingConstant.PLATFORM_BANNER_NUM),wrapper);
        return selectList(wrapper);
    }

    private Wrapper<EpgAggregationCategoryDTO> getBySelectCategoryInfo(EpgAggregationCategoryBO epgAggregationCategoryBO) {
        Wrapper<EpgAggregationCategoryDTO> wrapper = new EntityWrapper<>();
        EpgAggregationCategoryDTO epgAggregationCategoryDTO = new EpgAggregationCategoryDTO();
        BeanUtils.copyProperties(epgAggregationCategoryBO,epgAggregationCategoryDTO);
        wrapper.eq("status", epgAggregationCategoryBO.getStatus());
        wrapper.eq("is_publish", epgAggregationCategoryBO.getIsPublish());
        wrapper.eq("module_id", epgAggregationCategoryBO.getModuleId());
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgAggregationCategoryBO.getSort().intValue())//降序
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }

    /**
     * 创建频道
     * @param templateId
     * @param moduleId
     * @return
     */
    public Long creatPlateActionSection(Long templateId, Long moduleId,int sort,String name){
        EpgAggregationCategorySaveBO epgAggregationCategorySaveBO = new EpgAggregationCategorySaveBO();
        epgAggregationCategorySaveBO.setTempletId(templateId);
        epgAggregationCategorySaveBO.setModuleId(moduleId);
        epgAggregationCategorySaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationCategorySaveBO.setName(name);
        //分类
        epgAggregationCategorySaveBO.setSort(sort);
        return addAndReturnId(epgAggregationCategorySaveBO);
    }

    public List<Long> createPlateActiveSection(Long templateId, Long moduleId) {
        List<Long> idList = new ArrayList<>();
        EpgAggregationCategorySaveBO epgAggregationCategorySaveBO = new EpgAggregationCategorySaveBO();
        epgAggregationCategorySaveBO.setTempletId(templateId);
        epgAggregationCategorySaveBO.setModuleId(moduleId);
        epgAggregationCategorySaveBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgAggregationCategorySaveBO.setName("");
        //分类1
        epgAggregationCategorySaveBO.setSort(0);
        Long aLong = addAndReturnId(epgAggregationCategorySaveBO);
        idList.add(aLong);
        //分类2
        epgAggregationCategorySaveBO.setSort(1);
        Long aLong1 = addAndReturnId(epgAggregationCategorySaveBO);
        idList.add(aLong1);
        //分类3
        epgAggregationCategorySaveBO.setSort(2);
        Long aLong2 = addAndReturnId(epgAggregationCategorySaveBO);
        idList.add(aLong2);
        return idList;
    }
}
